package com.ztesoft.sca.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.ztesoft.sca.common.Constants;
import com.ztesoft.sca.model.QcSceneModel;
import com.ztesoft.sca.service.QcSceneService;
import com.ztesoft.sca.util.exception.ExceptionUtil;
import com.ztesoft.sca.util.message.ResponseUtils;
import com.ztesoft.sca.util.seq.SequenceCreator;

import net.sf.json.JSONObject;

@RestController
public class QcSceneController {
	private static final Logger logger = Logger.getLogger(QcSceneController.class);
    @Autowired
    private QcSceneService qcSceneService; 
	
	/*
	 * zxz 质检场景
	 * */
	@RequestMapping(value = "/api/qcScene.do",method = {RequestMethod.POST})
	public String QcSceneList(@RequestBody String requestBody,HttpServletRequest request) throws NumberFormatException, Exception {
        JSONObject jsonArr = JSONObject.fromObject(requestBody);
		logger.info("--------/api/qcScene.do--------");
		if(jsonArr == null){
			return ResponseUtils.feedback(Constants.INF_CODE_ERROR,Constants.INF_DESC_ERROR,Constants.INF_DESC_NULL_REQUESTBODY);
		}
		
		long id=0l;
		int from=0;//第1页开始
		int size=10; //每页大小  
        SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		List<QcSceneModel> list = new ArrayList<>();
		try {
			list = this.qcSceneService.listQcScene(id,from,size);
		} catch (Exception e) {
			 logger.error(" listQcScene occur an error!",e);
			 return ResponseUtils.feedback(Constants.INF_CODE_ERROR,Constants.INF_DESC_ERROR, ExceptionUtil.getMessage(e));
		}
        JSONArray json = new JSONArray();
        String statusStr="";
        String createTime="";
        for(QcSceneModel pLog : list){
            JSONObject jo = new JSONObject();
            jo.put("sceneId", pLog.getSceneId());
            jo.put("sceneName", pLog.getSceneName());
            
            if(!"".equals(pLog.getCreateTime())){
            	createTime=time.format(pLog.getCreateTime());
            }
            jo.put("createTime", createTime);
            jo.put("sceneDesc", pLog.getSceneDesc());
            if(pLog.getStatus()==1){
            	statusStr="激活";
            }else{
            	statusStr="未激活";
            }
            jo.put("status", statusStr);
            json.add(jo);
        }
        
		return json.toJSONString();
		//return ResponseUtils.feedback(Constants.INF_CODE_SUCC,Constants.INF_DESC_SUCC,json.toString());
	}
	
	/*
	 * zxz 新增质检场景
	 * */
	@RequestMapping(value = "/api/newQcScene.do",method = {RequestMethod.POST})
	public String newQcScene(@RequestBody String requestBody,HttpServletRequest request) throws NumberFormatException, Exception {
           JSONObject json = JSONObject.fromObject(requestBody);
	   		if(json == null){
				return ResponseUtils.feedback(Constants.INF_CODE_ERROR,Constants.INF_DESC_ERROR,Constants.INF_DESC_NULL_REQUESTBODY);
			}
	   		
	       long Id = 0l;
	       try {
	            Id = SequenceCreator.getSequence("SCA_SCENE_SEQ");
	       } catch (Exception e) {
	           logger.error("sequenceCreator obtain SCA_SCENE_SEQ occur an error!",e);
	           return ResponseUtils.feedback(Constants.INF_CODE_ERROR,Constants.INF_DESC_ERROR,ExceptionUtil.getMessage(e));
	       }
	       String sceneName = json.getString("sceneName");
	       String sceneDesc = json.getString("sceneDesc");
	       int status = json.getInt("status");
	       
	       QcSceneModel aiChat = new QcSceneModel();
	       aiChat.setSceneId(Id);
	       aiChat.setSceneName(sceneName);
	       aiChat.setSceneDesc(sceneDesc);
	       aiChat.setStatus(status);
	       aiChat.setCreateTime(new Date());
	       
	       try {
	           this.qcSceneService.insertQcScene(aiChat);
	       } catch (Exception e) {
	           logger.error("create qcScene  occur an error !",e);
	           return ResponseUtils.feedback(Constants.INF_CODE_ERROR,Constants.INF_DESC_ERROR,ExceptionUtil.getMessage(e));
	       }
	    return Constants.INF_DESC_SUCC;//ResponseUtils.feedback(Constants.INF_CODE_SUCC,Constants.INF_DESC_SUCC,"success");
	}
}
